<div id="main">
	<div id="main_left" class="f_left">
		<div id="bread"><?php echo $bread_crumbs; ?></div>
		<div class="cl_red">注意：超级管理员没有角色也拥有所有权限，这里只为演示，才有角色。一个用户可以有多个角色，这里演示；只有一个角色</div>
		<div id="grid_list">
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<tr class="th">
					<td width="50">UID</td>
					<td width="200">用户名</td>
					<td width="150">超级管理员</td>
					<td width="150">激活</td>
					<td width="150">角色</td>
					<td width="110">操作</td>
				</tr>
				<?php if(empty($user_list)): ?>
				<tr>
					<td colspan="3">还没有用户...</td>
				</tr>
				<?php else:?>
				<?php foreach($user_list as $list): ?>
				<tr>
					<td><?php echo $list['uid'];?></td>
					<td><?php echo $list['username'];?></td>
					<td <?php if($list['uid'] != 1) : ?>title="单击更改" class="cursor" id="manage_<?php echo $list['uid']; ?>" onclick="excute_ajax('changeUserManage', <?php echo $list['uid'];?>)"<?php endif;?>>
						<?php echo $list['is_manage'] ? '<span class="cl_green">√</span>' : '<span class="cl_red">×</span>';?>
					</td>
					<td <?php if($list['uid'] != 1) : ?>title="单击更改" class="cursor" id="activ_<?php echo $list['uid']; ?>" onclick="excute_ajax('changeUserActiv', <?php echo $list['uid'];?>)"<?php endif;?>><?php echo $list['is_activation'] ? '<span class="cl_green">√</span>' : '<span class="cl_red">×</span>';?></td>
					<td <?php if($list['uid'] != 1) : ?>title="双击更改" class="cursor" id="role_<?php echo $list['uid']; ?>" ondblclick="role('changeUserRole', <?php echo $list['uid'];?>)"<?php endif;?>><?php echo $list['role_name'] ?></td>
					<td><?php if($list['uid'] == 1):?>不能删除<?php else: ?><a href="javascript:;" class="delete" id="<?php echo $list['uid'];?>">删除</a><?php endif; ?></td>
				</tr>
				<?php endforeach ?>
				<?php endif ?>
			</table>
		</div>
		<?php echo $pages; ?>
		<script type="text/javascript">
		var html_option = '<select name="role_name" id="roles">';
			<?php if(!empty($user_role)):
					foreach($user_role as $role):
			?>
			html_option += '<option value="<?php echo $role['rid'];?>"><?php echo $role['role_name'];?></option>';
			<?php endforeach; endif;?>
			html_option += '</select>';

		var role = function(fun, uid)
		{
			td = $('#role_'+uid);
			if ($('#roles').attr('name') != 'role_name') {
				td.html(html_option);
				$('#roles').focus();
			}
			$('#roles').blur(function(){
				excute_ajax(fun,uid);
			});
		}
		var getAction = function(action)
		{
			if(action == 'changeUserManage')
				return '<?php echo D::createUrl('user/changeUserManage');?>';
			if(action == 'changeUserActiv')
				return '<?php echo D::createUrl('user/changeUserActiv');?>';
			if(action == 'changeUserRole')
				return '<?php echo D::createUrl('user/changeUserRole');?>';
		}
		var excute_ajax = function(fun, uid)
		{
			role_id = $('#roles').val() || '';
			action  = getAction(fun);
			$.ajax({
				url  : action,
				type : 'GET',
				dataType : 'json',
				data : {id : uid, rid : role_id},
				success : function(data)
				{
					if (data.code == 1) {
						alert(data.msg);
						return false;
					} else if (data.code == 0){
						if(fun == 'changeUserManage')
							changeStatus('#manage_'+uid);
						if(fun == 'changeUserActiv')
							changeStatus('#activ_'+uid);
						if(fun == 'changeUserRole')
							changeUserRole('#role_'+uid, data.name);
					}
				}
			});
		}
		function changeStatus(id)
		{
			span = $(id).children('span');
			if(span.text() == '×')
				span.text('√').addClass('cl_green').removeClass('cl_red');
			else
				span.text('×').addClass('cl_red').removeClass('cl_green');
		}
		function changeUserRole(id, name)
		{
			$(id).html(name);
		}
		$(function(){
			$('.delete').click(function(){
				_this = $(this);
				$.ajax({
					url  : '<?php echo D::createUrl('user/deleteUser'); ?>',
					type : 'GET',
					dataType : 'json',
					data : {id : _this.attr('id')},
					success : function(data)
					{
						if (data.code == 0) {
							_this.parent().parent().fadeOut(300);
						} else if(data.code == 1) {
							alert(data.msg);
							return false;
						}
					}
				});
			});
		});
		</script>
	</div>
	<div id="sidebar" class="f_left">
		{include file=block/sidebar_index}
	</div>
</div>